package com.emm.yixun.website.service.process;

import java.util.Date;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.common.enums.ApplyMessageProcessKeyEnums;
import com.emm.yixun.common.model.BusinessAbnorma;
import com.emm.yixun.common.model.dto.BusinessAbnormaDto;
import com.emm.yixun.common.model.dto.PayDepositDto;
import com.emm.yixun.common.model.dto.SignedDto;
import com.emm.yixun.common.model.dto.SubscribeDto;
import com.emm.yixun.common.model.enums.AuditStatusEnum;
import com.emm.yixun.common.model.enums.BusinessAbnormaEnum;
import com.emm.yixun.common.model.enums.BusinessTypeEnum;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.customer.service.PayDepositServiceFacade;
import com.emm.yixun.customer.service.SignedServiceFacade;
import com.emm.yixun.customer.service.SubscribeServiceFacade;
import com.emm.yixun.marketing.service.BusinessAbnormaServiceFacade;

@Service
public class ProcessServiceImpl implements ProcessService {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private RuntimeService runtimeService;
	
	@Autowired
	private BusinessAbnormaServiceFacade businessAbnormaServiceFacade;
	
	@Autowired
	private PayDepositServiceFacade payDepositServiceFacade;
	
	@Autowired
	private SubscribeServiceFacade subscribeServiceFacade;
	
	@Autowired
	private SignedServiceFacade signedServiceFacade;

	@Override
	public void updateBusinessCheckDate(String processInstanceId) {
		/*
		 * 1.查找流程对象
		 * 2.根据流程key处理业务审核时间
		 */
		ProcessInstance processInstance = queryProcessInstanceById(processInstanceId);
		if (null == processInstance)
			return;
		businessCheckDateHandle(processInstance.getProcessDefinitionKey(),processInstance.getBusinessKey(),processInstanceId);
	}

	private void businessCheckDateHandle(String key, String businessId,String processInstanceId) {
		if(StringUtils.isBlank(key) || StringUtils.isBlank(businessId)){
			return;
		}
		if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_1.getNo())) {
            //价格方案申请;无需处理
		} else if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_2.getNo())) {
           //价格方案失效审批申请
		   abnormaCheckDateHandle(businessId);
		} else if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_3.getNo())) {
           //认筹审批申请
		   payDepositCheckDateHandle(businessId);
		} else if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_4.getNo())) {
           //退筹审批申请
			abnormaCheckDateHandle(businessId);
		} else if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_5.getNo())) {
           //认购审批申请
			subscribeCheckDateHandle(businessId);
		} else if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_6.getNo())) {
           //退认购审批申请
			abnormaCheckDateHandle(businessId);
		} else if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_7.getNo())) {
           //认购换房审批申请
			abnormaCheckDateHandleByProcInstId(processInstanceId);
		} else if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_8.getNo())) {
           //更名审批申请
		   abnormaCheckDateHandle(businessId);
		} else if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_9.getNo())) {
          //延期签约审批(15天内)申请
			abnormaCheckDateHandle(businessId);
		} else if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_10.getNo())) {
          //延期签约审批(15到30天)申请
			abnormaCheckDateHandle(businessId);
		} else if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_11.getNo())) {
          //延期签约审批(30天以上)申请
			abnormaCheckDateHandle(businessId);
		} else if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_12.getNo())) {
		   //签约审批申请	
		   signedCheckDateHandle(businessId);
		} else if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_13.getNo())) {
		   //签约折扣审批申请	
			abnormaCheckDateHandleByProcInstId(processInstanceId);
		} else if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_14.getNo())) {
           //签约换房审批申请
			abnormaCheckDateHandleByProcInstId(processInstanceId);
		} else if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_15.getNo())) {
          //签约退房审批申请
			abnormaCheckDateHandle(businessId);
		} else if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_16.getNo())) {
		  //面积补差审批申请;无需处理
		} else if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_17.getNo())) {
          //认购折扣审批申请
		  abnormaCheckDateHandleByProcInstId(processInstanceId);
		} else if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_18.getNo())) {
          //应收款延期(15天内)
			abnormaCheckDateHandle(businessId);
		} else if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_19.getNo())) {
          //应收款延期(15到30天)
			abnormaCheckDateHandle(businessId);
		} else if (key.equals(ApplyMessageProcessKeyEnums.PROCESS_KEY_20.getNo())) {
          //应收款延期(30天以上)
			abnormaCheckDateHandle(businessId);
		}
	}
	//根据流程ID更新异常记录表的审核时间
	public void abnormaCheckDateHandleByProcInstId(String processInstanceId){
		if(StringUtils.isBlank(processInstanceId))return;
		BusinessAbnormaDto queryDto=new BusinessAbnormaDto();
		queryDto.setProcInstId(processInstanceId);
		ResponseList<BusinessAbnorma> responseList=businessAbnormaServiceFacade.findByDto(queryDto);
		if(null!=responseList && null!=responseList.getEntity() && responseList.getEntity().size()>0){
			for(BusinessAbnorma ba : responseList.getEntity()){
				abnormaCheckDateHandle(String.valueOf(ba.getId()));
			}
			
		}

	}

	private void signedCheckDateHandle(String id){
		if(StringUtils.isBlank(id))return;
		SignedDto updateDto = new SignedDto();
		updateDto.setId(Long.valueOf(id));
		updateDto.setCheckDate(new Date());
		Response<Integer> response=signedServiceFacade.updateByPrimaryKey(updateDto, true);
		if(!response.isSuccess()){
			logger.error("更新签约审核时间错误："+response.getResultMessage());
		}
	}


	private void subscribeCheckDateHandle(String id){
		if(StringUtils.isBlank(id))return;
		SubscribeDto updateDto = new SubscribeDto();
		updateDto.setId(Long.valueOf(id));
		updateDto.setCheckDate(new Date());
		Response<Integer> response=subscribeServiceFacade.updateByPrimaryKey(updateDto, true);
		if(!response.isSuccess()){
			logger.error("更新认购审核时间错误："+response.getResultMessage());
		}
	}
	private void payDepositCheckDateHandle(String id){
		if(StringUtils.isBlank(id))return;
		PayDepositDto updateDto = new PayDepositDto();
		updateDto.setId(Long.valueOf(id));
		updateDto.setCheckDate(new Date());
		Response<Integer> response=payDepositServiceFacade.updateByPrimaryKey(updateDto, true);
		if(!response.isSuccess()){
			logger.error("更新认筹审核时间错误："+response.getResultMessage());
		}
	}
	private void abnormaCheckDateHandle(String id){
		if(StringUtils.isBlank(id))return;
		BusinessAbnormaDto businessAbnormaDto=new BusinessAbnormaDto();
		businessAbnormaDto.setId(Long.valueOf(id));
		businessAbnormaDto.setCheckDate(new Date());
		Response<Integer> response=businessAbnormaServiceFacade.updateByPrimaryKey(businessAbnormaDto, true);
		if(!response.isSuccess()){
			logger.error("更新异常表审核时间错误："+response.getResultMessage());
		}
	}


	private ProcessInstance queryProcessInstanceById(String processInstanceId) {
		if (StringUtils.isBlank(processInstanceId)) {
			return null;
		}
		ProcessInstance processInstance = runtimeService
				.createProcessInstanceQuery()
				.processInstanceId(processInstanceId).singleResult();
		return processInstance;
	}

}
